package com.imonsoft.sqm.web.business.ipic_system;

import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.util.AjaxUtil;
import com.imonsoft.common.util.DateUtil;
import com.imonsoft.common.util.JsonUtil;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.service.business.ipic_system.IPICService;
import com.imonsoft.sqm.vo.business.ipic_system.PPMVo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/business/ipic_system/ppm")
public class PPMController {
    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    private SupplierService supplierService;
    @Autowired
    private IPICService ipicService;
    private DecimalFormat decimalFormat =new DecimalFormat(",###");//格式化类

    @RequestMapping("/listTabOneUI.do")
    public void listTabOneUI(ModelMap model) {
        PPMVo ppmVo = new PPMVo();
        ppmVo.setYear(DateUtil.getNowDate().substring(0,4));
        ppmVo.setMonth(DateUtil.getNowDate().substring(5, 7));
        model.put("entity", ppmVo);
    }

    @RequestMapping(value = "/ajaxTabOneList.do", method = RequestMethod.POST)
    public void ajaxTabOneList(HttpServletResponse response, Pager pager, PPMVo ppmVo) {
        DataGrid<PPMVo> dataGrid = new DataGrid<PPMVo>();
        try {
            if(StringUtils.isEmpty(ppmVo.getYear())){
                throw new SystemException("请选择年份！");
            }
            if(StringUtils.isEmpty(ppmVo.getMonth())){
                throw new SystemException("请选择月份！");
            }
            Map<String, Object> searchMap = new HashMap<String, Object>();//查询条件
            searchMap.put("year", ppmVo.getYear());
            searchMap.put("month", ppmVo.getMonth());
            Map<String,Object> resultMap=new HashMap<String, Object>();//计算结果
            List<PPMVo> ppmVoList = new ArrayList<PPMVo>();

            Supplier supplierSearch = new Supplier();
            supplierSearch.setState(ConstantSqm.STATUS_NORMAL);
            DataGrid<Supplier> supplierDataGrid = supplierService.getDataGrid(supplierSearch, pager);
            List<Supplier> supplierList = supplierDataGrid.getRows();
            for (Supplier supplier : supplierList) {
                PPMVo entity = new PPMVo();
                entity.setSupplierCode(supplier.getCode());
                entity.setSupplierName(supplier.getName());
                searchMap.put("supplierId", supplier.getId());

                //--月份数据--
                ipicService.calPPMMonthData(searchMap, resultMap);
                entity.setTotalNumM(decimalFormat.format(resultMap.get("totalNumM")));
                entity.setQuantityTM(decimalFormat.format(resultMap.get("quantityTM")));
                entity.setQuantityLM(decimalFormat.format(resultMap.get("quantityLM")));
                entity.setPpmTM(decimalFormat.format(resultMap.get("ppmTM")));
                entity.setPpmLM(decimalFormat.format(resultMap.get("ppmLM")));
                entity.setPpmM(decimalFormat.format(resultMap.get("ppmM")));
                //--累计数据--
                ipicService.calPPMAccumulateData(searchMap, resultMap);
                entity.setTotalNumA(decimalFormat.format(resultMap.get("totalNumA")));
                entity.setQuantityTA(decimalFormat.format(resultMap.get("quantityTA")));
                entity.setQuantityLA(decimalFormat.format(resultMap.get("quantityLA")));
                entity.setPpmTA(decimalFormat.format(resultMap.get("ppmTA")));
                entity.setPpmLA(decimalFormat.format(resultMap.get("ppmLA")));
                entity.setPpmA(decimalFormat.format(resultMap.get("ppmA")));
                //--滚动数据--
                calRollingData(searchMap,resultMap);
                entity.setPpmTR(decimalFormat.format(resultMap.get("ppmTR")));
                entity.setPpmLR(decimalFormat.format(resultMap.get("ppmLR")));
                entity.setPpmR(decimalFormat.format(resultMap.get("ppmR")));

                ppmVoList.add(entity);
            }
            dataGrid.setRows(ppmVoList);
            dataGrid.setTotal(supplierDataGrid.getTotal());
        } catch (SystemException se) {
            logger.info(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
    }
    private void calRollingData(Map<String,Object> searchMap,Map<String,Object> resultMap){
        String month=(String)searchMap.get("month");
        if(month.equals("01")){
            searchMap.put("monthBegin","01");
            searchMap.put("monthEnd","01");
        }else if(month.equals("02")){
            searchMap.put("monthBegin","01");
            searchMap.put("monthEnd","02");
        }else{
            searchMap.put("monthBegin",StringUtil.paddingZero((Integer.parseInt(month)-2)+"",2));
            searchMap.put("monthEnd",month);
        }
        double totalNumR=ipicService.getIncomingAmountRolling(searchMap);
        searchMap.put("claimType", ConstantSqm.CLAIM_TYPE_TECHNICAL);
        double quantityTR=ipicService.getAmountNotQualifiedRolling(searchMap);
        searchMap.put("claimType", ConstantSqm.CLAIM_TYPE_LOGISTIC);
        double quantityLR=ipicService.getAmountNotQualifiedRolling(searchMap);
        double ppmTR=0.0;
        double ppmLR=0.0;
        double ppmR=0.0;
        if(totalNumR!=0.0){
            ppmTR=(quantityTR*1000000.0)/totalNumR;
            ppmLR=(quantityLR*1000000.0)/totalNumR;
            ppmR=ppmTR+ppmLR;
        }
        resultMap.put("totalNumR",totalNumR);
        resultMap.put("quantityTR",quantityTR);
        resultMap.put("quantityLR",quantityLR);
        resultMap.put("ppmTR",ppmTR);
        resultMap.put("ppmLR",ppmLR);
        resultMap.put("ppmR",ppmR);
    }






    @RequestMapping("/listTabTwoUI.do")
    public void listTabTwoUI(ModelMap model) {
        PPMVo ppmVo = new PPMVo();
        ppmVo.setYear(DateUtil.getNowDate().substring(0,4));
        model.put("entity", ppmVo);
    }
    @RequestMapping(value = "/ajaxTabTwoList.do", method = RequestMethod.POST)
    public void ajaxTabTwoList(HttpServletResponse response, Pager pager, PPMVo ppmVo) {
        DataGrid<PPMVo> dataGrid = new DataGrid<PPMVo>();
        try {
            if(null==ppmVo.getSupplierId()){
                throw new SystemException("请选择供应商！");
            }
            if(StringUtils.isEmpty(ppmVo.getYear())){
                throw new SystemException("请选择年份！");
            }
            Map<String, Object> searchMap = new HashMap<String, Object>();//查询条件
            searchMap.put("supplierId", ppmVo.getSupplierId());
            searchMap.put("year", ppmVo.getYear());
            Map<String,Object> resultMap=new HashMap<String, Object>();//计算结果

            List<PPMVo> ppmVoList = new ArrayList<PPMVo>();
            for(int monthTemp=1;monthTemp<=12;monthTemp++){
                PPMVo entity = new PPMVo();
                entity.setMonth(StringUtil.paddingZero(""+monthTemp,2));

                searchMap.put("month", StringUtil.paddingZero(""+monthTemp,2));
                //--月份数据--
                ipicService.calPPMMonthData(searchMap, resultMap);
                entity.setTotalNumM(decimalFormat.format(resultMap.get("totalNumM")));
                entity.setQuantityTM(decimalFormat.format(resultMap.get("quantityTM")));
                entity.setQuantityLM(decimalFormat.format(resultMap.get("quantityLM")));
                entity.setPpmTM(decimalFormat.format(resultMap.get("ppmTM")));
                entity.setPpmLM(decimalFormat.format(resultMap.get("ppmLM")));
                entity.setPpmM(decimalFormat.format(resultMap.get("ppmM")));
                //--累计数据--
                ipicService.calPPMAccumulateData(searchMap, resultMap);
                entity.setTotalNumA(decimalFormat.format(resultMap.get("totalNumA")));
                entity.setQuantityTA(decimalFormat.format(resultMap.get("quantityTA")));
                entity.setQuantityLA(decimalFormat.format(resultMap.get("quantityLA")));
                entity.setPpmTA(decimalFormat.format(resultMap.get("ppmTA")));
                entity.setPpmLA(decimalFormat.format(resultMap.get("ppmLA")));
                entity.setPpmA(decimalFormat.format(resultMap.get("ppmA")));
                //--滚动数据--
                calRollingData(searchMap,resultMap);
                entity.setTotalNumR(decimalFormat.format(resultMap.get("totalNumR")));
                entity.setQuantityTR(decimalFormat.format(resultMap.get("quantityTR")));
                entity.setQuantityLR(decimalFormat.format(resultMap.get("quantityLR")));
                entity.setPpmTR(decimalFormat.format(resultMap.get("ppmTR")));
                entity.setPpmLR(decimalFormat.format(resultMap.get("ppmLR")));
                entity.setPpmR(decimalFormat.format(resultMap.get("ppmR")));

                ppmVoList.add(entity);
            }
            dataGrid.setRows(ppmVoList);
            dataGrid.setTotal(12);
        } catch (SystemException se) {
            logger.info(se.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        AjaxUtil.renderJSON(response, JsonUtil.bean2json(dataGrid));
    }

    @RequestMapping("/tabUI.do")
    public void tabUI() {
    }
}
